/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package matching;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import matching.xml.MatchingXML;
import ontology.Ontology;
import org.jdom.Attribute;
import org.jdom.Element;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLObjectProperty;


/**
 *
 * @author livia
 */
public class Matching {
    
    public Ontology source;
    public Ontology target;
    public String sourceName;
    public String targetName;
    public ArrayList<Match> matchings;
    public ArrayList<ClassMatch> classMatchings;
    public ArrayList<DataPropertyMatch> dataPropMatchings;
    public ArrayList<ObjectPropertyMatch> objPropMatchings;

    public Matching(Ontology source, Ontology target, String sourceName, String targetName) {
        this.source = source;
        this.target = target;
        this.sourceName=sourceName;
        this.targetName = targetName;
        this.matchings = new ArrayList<Match>();
        this.classMatchings = new ArrayList<ClassMatch>();
        this.dataPropMatchings = new ArrayList<DataPropertyMatch>();
        this.objPropMatchings = new ArrayList<ObjectPropertyMatch>();
    }

   
    public void addMatching(Match m) throws Exception{
       	if(m instanceof ClassMatch)classMatchings.add((ClassMatch) m);
        else if(m instanceof DataPropertyMatch)dataPropMatchings.add((DataPropertyMatch) m);
        else if(m instanceof ObjectPropertyMatch)objPropMatchings.add((ObjectPropertyMatch) m);
        matchings.add(m);
    } 

    public Collection<Match> getMatchings(){
        return matchings;
    }

    public boolean hasMatching(OWLClass c1, OWLClass c2){
        for (ClassMatch match : classMatchings) {
            if((match.getSource().getElement().equals(c1)&& match.getTarget().getElement().equals(c2))||
              (match.getSource().getElement().equals(c2)&& match.getTarget().getElement().equals(c1)))return true;
        }
        return false;
    }
    public boolean hasMatching(OWLDataProperty d1, OWLDataProperty d2){
        for (DataPropertyMatch match : dataPropMatchings) {
            if((match.getSource().getElement().equals(d1)&& match.getTarget().getElement().equals(d2))||
              (match.getSource().getElement().equals(d2)&& match.getTarget().getElement().equals(d1)))return true;
        }
        return false;
    }

    public boolean hasMatching(OWLObjectProperty o1, OWLObjectProperty o2){
        for (ObjectPropertyMatch match : objPropMatchings) {
            if((match.getSource().getElement().equals(o1)&& match.getTarget().getElement().equals(o2))||
              (match.getSource().getElement().equals(o2)&& match.getTarget().getElement().equals(o1)))return true;
        }
        return false;
    }


    /*
    private boolean checkedClass(String cl, OWLOntology onto){
        for (OWLClass cls : onto.getReferencedClasses()){
             if(cl.equals(cls.toString())) return true;
        }
        return false;
    }

    //verifica se o metodo para pegar propriedades eh esse
    private boolean checkedProperty(OWLDataProperty prop, OWLOntology onto){
    	
    	for (OWLDescription cl : prop.getDomains(onto)) {
             
        }
        return false;
    }*/

    public ArrayList<Match> getMatching(){
        return matchings;
    }

    public boolean hasMatch(Match m){
        Match matching;
        for (Iterator<Match> it = matchings.iterator(); it.hasNext();) {
            matching = it.next();
            if(m.equals(matching))return true;
        }
        return false;
    }
    
   public void print() {
        for (Match m : matchings) {
                System.out.print(m.source.getElement()+
                                " "+m.source.getContext()+
                                " "+m.source.getRestriction()+"  ");
                System.out.println(m.target.getElement()+" "
                                +m.target.getContext()+" "
                                +m.target.getRestriction());
        }

   }

   public Element toElementXml() {
       Element root = new Element("matching");
       List<Attribute> listAtrib = new LinkedList<Attribute>();
       root.addNamespaceDeclaration(MatchingXML.uriXmlns);
       listAtrib.add(new Attribute(MatchingXML.sourceOntology, sourceName));
       listAtrib.add(new Attribute(MatchingXML.targetOntology, targetName));
       root.setAttributes(listAtrib);
       for (Match match : matchings) {
           root.addContent(match.toElementXML());
       }
       return root;
   }
}
